﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rung.EBook.Common.Exceptions;

namespace Rung.EBook.Common.Model
{
    [Serializable]
    public class BookBase
    {
        public BookBase()
        {
            this.Chapters = new List<ChapterBase>();
        }

        protected Encoding mEncoding = Encoding.Unicode;
        protected DateTime mDate = DateTime.Now;
        protected ProjectBM mProjectBM;
        protected IList<ChapterBase> mChapters;

        /// <summary>
        /// 书名
        /// </summary>
        public string Title
        {
            get;
            set;
        }

        /// <summary>
        /// 书籍保存的目录
        /// </summary>
        public string FilePath
        {
            get;
            set;
        }

        public Encoding Encoding
        {
            get
            {
                return this.mEncoding;
            }
        }

        /// <summary>
        /// 作者
        /// </summary>
        public string Author
        {
            get;
            set;
        }

        /// <summary>
        /// 发行日期
        /// </summary>
        public DateTime Date
        {
            get { return this.mDate; }
        }

        /// <summary>
        /// 所有章节
        /// </summary>
        public IList<ChapterBase> Chapters
        {
            get { return this.mChapters; }
            set { this.mChapters = value; }
        }

        /// <summary>
        /// 添加一个章节的内容。
        /// 为维护章节的有序性，建议使用此方法而不是直接调用 Chapters 属性的 Add 方法。
        /// </summary>
        public virtual void AppendChapter(ChapterBase chapter)
        {
            chapter.Index = this.mChapters.Count;
            this.mChapters.Add(chapter);
        }

        /// <summary>
        /// 移除一个章节的内容。
        /// 为维护章节的有序性，建议使用此方法而不是直接调用 Chapters 属性的 Remove 方法。
        /// </summary>
        public void RemoveChapter(ChapterBase chapter)
        {
            int index = chapter.Index;
            if (this.mChapters.Remove(chapter))
            {
                for (int i = chapter.Index; i < this.mChapters.Count; i++)
                {
                    this.mChapters[i].Index = i;
                }
            }
        }

        /// <summary>
        /// 生成项目
        /// </summary>
        public ProjectBM ToProject()
        {
            return new ProjectBM
            {
                Book = this,
                BookClass = this.GetType().ToString(),
                BuildTime = DateTime.Now
            };
        }

        /// <summary>
        /// 保存项目
        /// </summary>
        public void SaveAsProject(string filePath)
        {
            if (string.IsNullOrEmpty(this.Title)) throw (new MetaInfoNullException("标题不能为空。"));

            filePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, filePath, this.Title + ".ebp");
            this.ToProject().Save(filePath);
        }

        public virtual void LoadByProject(string filePath)
        {
            //filePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, filePath, this.Title + ".ebp");
            if (System.IO.File.Exists(filePath) == false) throw (new ProjectFileNotFunndException("项目文件不存在。"));

            this.mProjectBM = ProjectBM.Load(filePath);
            if (this.mProjectBM == null) throw (new ProjectFormateException("项目文件格式错误（1）。"));

            this.mDate = this.mProjectBM.Book.Date;
            this.mEncoding = this.mProjectBM.Book.Encoding;
            this.mChapters = this.mProjectBM.Book.Chapters;
        }

        /// <summary>
        /// 由父类生成子类。
        /// </summary>
        /// <param name="bBase"></param>
        public virtual void FromBookBase(BookBase bBase)
        { }
    }
}
